﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ditto.Helpers;

namespace Ditto.Models
{
    public class UserRepository : IUserRepository
    {
        DittoEntities db = new DittoEntities();

        public UserRelationship FindRelationship(string requestingUser, string confirmingUser)
        {
            return db.UserRelationships.Where(r => r.RequestingUserID == requestingUser && r.ConfirmingUserID == confirmingUser).FirstOrDefault();
        }

        public void AddRelationship(UserRelationship rel)
        {
            db.UserRelationships.AddObject(rel);
        }

        public void DeleteRelationship(UserRelationship rel)
        {
            db.UserRelationships.DeleteObject(rel);
        }

        public IQueryable<User> FindConfirmedFriends(string user)
        {
            var requested = from r in db.UserRelationships.Include("User")
                            where r.RequestingUserID == user && r.IsConfirmed == true
                            select r.User1;
            var confirmed = from c in db.UserRelationships.Include("User")
                            where c.ConfirmingUserID == user && c.IsConfirmed == true
                            select c.User;

            var friends = requested.Union(confirmed);

            return friends;
        }

        public IQueryable<UserRelationship> FindRequestingFriends(string user)
        {
            var requested = from r in db.UserRelationships.Include("User")
                            where r.ConfirmingUserID == user && r.IsConfirmed == false
                            select r;

            return requested;
        }

        public User GetUser(string id)
        {
            return db.Users.SingleOrDefault(d => d.UserID == id);
        }

        public void Add(User user)
        {
            db.Users.AddObject(user);
        }

        public void Delete(User user)
        {
            db.Users.DeleteObject(user);
        }

        public void Save()
        {
            db.SaveChanges();
        }

        public Reputation GetUserReputation(string id)
        {
            Reputation rep = new Reputation();

            //User user = db.Users.Single(u => u.UserID == id);
            //rep.Rating = user.UserRating ?? 0;

            var comments = db.TransactionComments.Where(uc => uc.TransactionRating != null && (uc.Transaction.TransactingUserID == id || uc.Transaction.Item.PostingUserID == id));
            int count = comments.Count();
            if (count > 0)
                rep.Rating = comments.Sum(c => (double)c.TransactionRating) / count;

            var trans = db.Transactions.Where(t => t.TransactingUserID == id || t.Item.PostingUserID == id);

            rep.VolumeTransacted = trans.Count();
            rep.ValueTransacted = Convert.ToDouble(trans.Sum(t => t.Item.OriginalValue));


            if (rep.VolumeTransacted == 0)
            {
                rep.LevelStatus = ReputationHelper.Ranks.ElementAt(0);
            }
            else if (rep.VolumeTransacted < 10)
            {
                rep.LevelStatus = ReputationHelper.Ranks.ElementAt(1);
            }
            else if (rep.VolumeTransacted < 30)
            {
                rep.LevelStatus = ReputationHelper.Ranks.ElementAt(2);
            }
            else
            {
                rep.LevelStatus = ReputationHelper.Ranks.ElementAt(3);
            }

            return rep;
        }
    }
}